function lengthen(width, scale, direction, increment) {
    var materialWidth = width;
    var drawingScale = scale;
    // 转换单位
    // 转换成米
    function pointToM(numIn) {
        return (((numIn / 72) * 25.4) / 1000) * drawingScale;
    }

    // 转换成点
    function MTopoint(numIn) {
        return (((numIn * 1000) / 25.4) * 72) / drawingScale;
    }

    // 格式化数字
    function formatNumForJudge(numIn) {
        return parseFloat(trimEndZero(formatNum(numIn, 2)));
    }

    if (documents.length > 0) {
        var activeDocument = app.activeDocument;
        var elementsSelected = activeDocument.selection;
        if (elementsSelected.length > 0) {
            for (i = 0; i < elementsSelected.length; i++) {
                var targetItem = elementsSelected[i];

                // 初始化变量
                var L2 = MTopoint(materialWidth);
                var L1 = targetItem.length / 2 - L2;
                var H = targetItem.height;
                var W = targetItem.width;
                var Origin_X = targetItem.left;
                var Origin_Y = targetItem.top;
                var A_X = Origin_X;
                var A_Y = Origin_Y - Math.sin(calculateRadian(targetItem)) * L2;
                var B_X = Origin_X + Math.cos(calculateRadian(targetItem)) * L2;
                var B_Y = Origin_Y;
                var C_X = Origin_X + Math.sin(calculateRadian(targetItem)) * L1;
                var C_Y = Origin_Y - H;
                var D_X = Origin_X + W;
                var D_Y = Origin_Y - H + Math.sin(calculateRadian(targetItem)) * L2;
                // 记录初始弧度
                var radian = calculateRadian(targetItem);

                var ifTrigger = false;
                var triggerStatue = 0;
                // 判断是否触发
                switch (direction) {
                    // 2486分别代表下左上右
                    case 2:
                        // 根据角度，决定如何计算新的原点坐标
                        var angle = calculateAngle(targetItem);
                        // alert(angle);
                        if (angle > 360) {
                            angle %= 360;
                        }
                        // alert(angle);
                        // 不支持类似“0 <= angle <= 90”的写法
                        if ((angle >= 45 && angle < 135) || (angle >= 225 && angle < 315)) {
                            ifTrigger = true;
                            triggerStatue = 2;
                        }
                        break;
                    case 4:
                        var angle = calculateAngle(targetItem);
                        // alert(angle);
                        if (angle > 360) {
                            angle %= 360;
                        }
                        // alert(angle);
                        if ((angle >= 135 && angle < 225) || (angle >= 0 && angle < 45) || (angle >= 315 && angle < 360) || 
                        (angle >= -45 && angle < 0) || (angle >= -225 && angle < -135) || (angle >= -360 && angle < -315)) {
                            ifTrigger = true;
                            triggerStatue = 4;
                        }
                        break;
                    case 5:
                        ifTrigger = true;
                        triggerStatue = 5;
                        // 输出调试信息
                        var debugInfo = "L1: " + L1 + "\n" + "L2: " + L2 + "\n" +
                            "H: " + H + "\n" + "W: " + W + "\n" +
                            "Origin_X: " + Origin_X + "\n" + "Origin_Y: " + Origin_Y + "\n" +
                            "A_X: " + A_X + "\n" + "A_Y:" + A_Y + "\n" +
                            "B_X: " + B_X + "\n" + "B_Y:" + B_Y + "\n" +
                            "C_X: " + C_X + "\n" + "C_Y:" + C_Y + "\n" +
                            "D_X: " + D_X + "\n" + "D_Y:" + D_Y + "\n" +
                            "角度: " + calculateAngle(targetItem);
                        alert(debugInfo);
                        break;
                    case 6:
                        var angle = calculateAngle(targetItem);
                        // alert(angle);
                        if (angle > 360) {
                            angle %= 360;
                        }
                        // alert(angle);
                        if ((angle >= 135 && angle < 225) || (angle >= 0 && angle < 45) || (angle >= 315 && angle < 360) || 
                        (angle >= -45 && angle < 0) || (angle >= -225 && angle < -135) || (angle >= -360 && angle < -315)) {
                            ifTrigger = true;
                            triggerStatue = 4;
                        }
                        break;
                    case 8:
                        var angle = calculateAngle(targetItem);
                        // alert(angle);
                        if (angle > 360) {
                            angle %= 360;
                        }
                        // alert(angle);
                        if ((angle >= 45 && angle < 135) || (angle >= 225 && angle < 315)) {
                            ifTrigger = true;
                            triggerStatue = 8;
                        }
                        break;
                }
                if (ifTrigger) {
                    // 逆转物体
                    var rotateMatrix = app.getRotationMatrix(calculateAngle(targetItem) * -1);
                    targetItem.transform(rotateMatrix);
                    // 更新弧度
                    if (targetItem.tags.length !== 0) {
                        targetItem.tags[0].value = 0;
                    }
                    // 加长物体
                    if (formatNumForJudge(targetItem.width) === formatNumForJudge(L2)) {
                        targetItem.height = L1 + MTopoint(increment / 100);
                    } else if (formatNumForJudge(targetItem.height) === formatNumForJudge(L2)) {
                        targetItem.width = L1 + MTopoint(increment / 100);
                    }
                    // 转回原始角度
                    rotateMatrix = app.getRotationMatrix(calculateAngleFromRadian(radian));
                    targetItem.transform(rotateMatrix);
                    // 更新弧度
                    if (targetItem.tags.length !== 0) {
                        targetItem.tags[0].value = radian;
                    }
                    // 根据需求更新原点坐标
                    switch (triggerStatue) {
                        case 2:
                            if ((angle >= 90 && angle < 135) || (angle >= 270 && angle < 315)) {
                                targetItem.left = Origin_X;
                                targetItem.top = Origin_Y;
                            } else {
                                //
                            }
                            break;
                        case 4:
                            //
                            break;
                        case 5:
                            //
                            break;
                        case 6:
                            //
                            break;
                        case 8:
                            //
                            break;
                    }
                    // targetItem.selected = false;
                }
            }
        } else {
            alert("请先选中一个物体!");
        }
    } else {
        alert("请先打开一个文档!");
    }
}

function formatNum(numIn, accuracyIn) {
    var tempNum = numIn.toFixed(accuracyIn + 2).slice(0, -1);
    var tempNumString = "" + tempNum;
    var lastNum = parseInt(tempNumString.charAt(tempNumString.length - 1));
    if (lastNum != 0) {
        var zeroString = "";
        var tempString = "";
        for (k = 1; k < accuracyIn; k++) {
            zeroString += "0";
        }
        tempString = "0." + zeroString + "1";
        // 此时area为字符串
        numIn = numIn.toFixed(accuracyIn + 1).slice(0, -1);
        numIn = parseFloat(numIn) + parseFloat(tempString);
    } else {
        numIn = numIn.toFixed(accuracyIn + 1).slice(0, -1);
    }
    return numIn;
}

function trimEndZero(numIn) {
    var originString = "" + numIn;
    numIn = parseFloat(originString);
    return numIn;
}

// 计算弧度
function calculateRadian(itemIn) {
    var radian;
    if (itemIn.tags.length !== 0) {
        radian = itemIn.tags[0].value;
    } else {
        radian = 0;
    }
    return radian;
}

// 弧度转角度
function calculateAngle(itemIn) {
    return (180 * calculateRadian(itemIn)) / Math.PI;
}

function calculateAngleFromRadian(valueIn) {
    return (180 * valueIn) / Math.PI;
}

function alertOriginAndAngle(itemIn) {
    var rotation = calculateAngle(calculateRadian(itemIn));

    var originX = itemIn.left;
    var originY = itemIn.top;

    alert("原点坐标为：(" + originX + ", " + originY + ")\n" + "角度：" + rotation);
}
